#!/usr/bin/with-contenv sh

set -u # Treat unset variables as an error.

log() {
    echo "[$(basename "$(pwd)")] $*"
}

get_xvfb_pid() {
    PID="$(ps -A -o pid,args | \
           grep -vw grep | \
           grep -w '/usr/bin/Xvfb' | \
           tr -s ' ' | \
           cut -d' ' -f2)"
    echo "${PID:-UNSET}"
}

# Make sure to kill any running instance of xvfb.
PID="$(get_xvfb_pid)"
if [ "$PID" != "UNSET" ]; then
    log "WARNING: an instance is already running?!  Terminating it..."
    kill -15 $PID #SIGTERM
    for i in $(seq 1 5); do
        sleep 1
        PID="$(get_xvfb_pid)"
        [ "$PID" != "UNSET" ] || break
    done
    if [ "$PID" != "UNSET" ]; then
        log "WARNING: previous instance still running after 5 seconds.  Killing it..."
        kill -9 $PID #SIGKILL
        sleep 1
    fi
fi

# Make sure to remove the X server's lock file.
rm -f /tmp/.X$(echo $DISPLAY | cut -d':' -f2)-lock

log "starting..."

exec 2>&1 /bin/s6-notifyoncheck -d /usr/bin/Xvfb $DISPLAY -screen 0 ${DISPLAY_WIDTH}x${DISPLAY_HEIGHT}x24

# vim: set ft=sh :
